package com.eastjet.equipment.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastjet.equipment.service.DeviceInspectionprojectService;
import com.eastjet.equipment.service.DeviceInspectiontaskResultService;
import com.eastjet.equipment.service.DeviceRulesService;
import com.eastjet.equipment.service.SelectTypeService;
import com.eastjet.equipment.util.CodeRules;
import com.eastjet.maintenance.domain.DeviceInspectionproject;
import com.eastjet.maintenance.domain.DeviceInspectiontaskResult;
import com.eastjet.maintenance.domain.DeviceRules;
import com.eastjet.maintenance.domain.SelectType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@CrossOrigin
@RestController
@RequestMapping("/inspectionpro")
public class DeviceInspectionprojectController {
    @Autowired
    private DeviceInspectionprojectService deviceInspectionprojectService;

    @Autowired
    private DeviceRulesService deviceRulesService;


    @Autowired
    private CodeRules codeRules;


    @GetMapping("/query")
    public Page<DeviceInspectionproject> getSupplierList(@RequestParam(defaultValue = "1") int page,
                                                         @RequestParam(defaultValue = "2") int size,
                                                         @RequestParam(defaultValue = "") String condition
    ) {
        System.out.println("condition:" + condition);
        System.out.println("page:" + page);
        return deviceInspectionprojectService.getProjectList(page, size, condition);
    }

    @GetMapping("/updateState")
    public int updateState(@RequestParam(value = "id", required = false, defaultValue = "0") Integer id, @RequestParam(value = "status", required = false, defaultValue = "0") Integer status) {

        System.out.println("status:" + status);
        System.out.println("id:" + id);

        int update = deviceInspectionprojectService.updateState(id, status);
        return update;
    }

    @GetMapping("/delPro")
    public int delPro(@RequestParam(value = "id", required = false, defaultValue = "0") Integer id, @RequestParam(value = "isDelete", required = false, defaultValue = "0") Integer isDelete) {

        System.out.println("isDelete:" + isDelete);
        System.out.println("id:" + id);

        int del = deviceInspectionprojectService.del(id, isDelete);
        return del;
    }

    public String code() {
        List<DeviceRules> list = deviceRulesService.getDeviceRulesByTableName("device_inspectionproject");
        String s = codeRules.codeRules(list);
        return s;
    }

    @RequestMapping("/addInspectionPro")
    public String addInspectionPro(@RequestParam(defaultValue = "") Map<String, Object> map) {

        DeviceInspectionproject dp = new DeviceInspectionproject();
        dp.setInspectionprojectState(Integer.valueOf(map.get("inspectionprojectState").toString()));
        dp.setInspectionprojectName((String) map.get("inspectionprojectName"));
        dp.setInspectionprojectTypeId((String) map.get("inspectionprojectTypeId"));
        dp.setInspectionprojectContent((String) map.get("inspectionprojectContent"));
        dp.setInspectionprojectResult(Integer.valueOf(map.get("inspectionprojectResult").toString()));
        dp.setInspectionprojectIsresult((String) map.get("inspectionprojectIsresult"));
        dp.setIsDelete(0);
        dp.setNotse((String) map.get("notse"));
        dp.setCode(code());
        dp.setInsertTime(new Date());
        dp.setUpdateTime(new Date());

        System.out.println("pro:" + dp);
        return deviceInspectionprojectService.projectAdd(dp);
    }

    @RequestMapping("/addSe")
    public String addSe(@RequestParam(defaultValue = "") Map<String, Object> map) {
        System.out.println("se:" + map);
        SelectType st = new SelectType();
        st.setOptions((String) map.get("value"));
        st.setCode(code());
        switch (Integer.valueOf((String) map.get("choose"))) {
            case 1:
                st.setChoose(0);
                break;
            case 2:
                st.setChoose(0);
                break;
            case 3:
                st.setChoose(1);
                st.setJudgment(Integer.valueOf((String) map.get("jugement")));
                break;
            case 4:
                st.setChoose(2);
                st.setJudgment(2);
                break;
            default:
                st.setChoose(0);
                break;
        }
        return deviceInspectionprojectService.SelectAdd(st);
    }

    @RequestMapping("/addRes")
    public String addRes(@RequestParam(defaultValue = "") Map<String, Object> map) {
        System.out.println("res:" + map);
        DeviceInspectiontaskResult sr = new DeviceInspectiontaskResult();
        sr.setIsDelete(0);
        sr.setCode(code());
        switch (Integer.valueOf((String) map.get("choose"))) {
            case 1:
                sr.setChoose(0);
                sr.setLowerLimit(Integer.valueOf((String) map.get("resultLower")));
                sr.setUpperLimit(Integer.valueOf((String) map.get("resultUpper")));
                sr.setResultName((String) map.get("resultName1"));
                break;
            case 2:
                sr.setChoose(0);
                sr.setNotse((String) map.get("notse"));
                sr.setResultName((String) map.get("resultName2"));
                break;
            case 3:
                sr.setChoose(1);
                sr.setResultName((String) map.get("resultName3"));
                break;
            case 4:
                sr.setChoose(2);
                sr.setResultName((String) map.get("resultName4"));
                break;
            default:
                sr.setChoose(0);
                break;
        }

        System.out.println("addres:" + sr);
        return deviceInspectionprojectService.resultAdd(sr);
    }

    @RequestMapping("/update")
    public String update(@RequestBody Map<String, Object> map) {
        System.out.println("update:" + map);
        DeviceInspectionproject deviceInspectionproject = new DeviceInspectionproject();
        deviceInspectionproject.setInspectionprojectName((String) map.get("inspectionprojectName"));
        deviceInspectionproject.setInspectionprojectState(Integer.valueOf((Integer) map.get("inspectionprojectState")));
        deviceInspectionproject.setNotse((String) map.get("notse"));
        deviceInspectionproject.setInspectionprojectContent((String) map.get("inspectionprojectContent"));
        deviceInspectionproject.setInspectionprojectTypeId((String) map.get("inspectionprojectTypeId"));
        deviceInspectionproject.setCode((String) map.get("code"));
        return deviceInspectionprojectService.update(deviceInspectionproject);
    }
}
